package com.chixing.service.impl;

import com.chixing.mapper.CarMapper;
import com.chixing.mapper.StoreCarMapper;
import com.chixing.pojo.Car;
import com.chixing.pojo.CarExample;
import com.chixing.pojo.StoreCar;
import com.chixing.pojo.StoreCarExample;
import com.chixing.service.HotCarService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HotCarServiceImpl implements HotCarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private StoreCarMapper storeCarMapper;
    @Override
    public ServerResponse getHotCar(String carType, String carBrand) {
        CarExample carExample = new CarExample();
        StoreCarExample storeCarExample = new StoreCarExample();
        if(carType.equals("-1"))
            carType = null;
        if(carBrand.equals("-1"))
            carBrand = null;

        storeCarExample.setOrderByClause("car_sale_count desc");
        List<StoreCar> storeCarList = storeCarMapper.selectByExample(storeCarExample);
        List<Car> carList = new ArrayList<>();
        for(StoreCar storeCar : storeCarList) {
            CarExample.Criteria criteria = carExample.createCriteria();
            if (carType != null)
                criteria.andCarTypeEqualTo(carType);
            if (carBrand != null)
                criteria.andCarBrandEqualTo(carBrand);
            criteria.andCarIdEqualTo(storeCar.getCarId());
            List<Car> carList1 = carMapper.selectByExample(carExample);
            if(carList1.size() > 0) {
                Car car = carList1.get(0);
                if(!carList.contains(car))
                    carList.add(carList1.get(0));
            }
            carExample.clear();
        }
        if(carList.size() > 0 )
            return ServerResponse.getSuccess(carList);
        return ServerResponse.getFail(null);
    }
}
